package com.Blog.www.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class 测试正则表达式的 {
    public static void main(String[] args) {

        String input = "www.cgjmail#$%@163.net";
        Pattern p = Pattern.compile("^//.|^//@");
        Matcher m = p.matcher(input);
        System.out.println("开始执行第一次校验");
        if (m.find()) {
            System.out.println("触发了");
            System.err.println("EMAIL地址不能以'.'或'@'作为起始字符");
        }
//检测是否以"www."为起始
        System.out.println("这是第二部分的校验");
        p = Pattern.compile("^www//.");
        m = p.matcher(input);
        if (m.find()) {
            System.out.println("EMAIL地址不能以'www.'起始");
        }
//检测是否包含非法字符
        p = Pattern.compile("[^A-Za-z0-9//.//@_//-~#]+");
        m = p.matcher(input);
        StringBuffer sb = new StringBuffer();
        boolean result = m.find();
        boolean deletedIllegalChars = false;
        while (result) {
//如果找到了非法字符那么就设下标记
            deletedIllegalChars = true;
//如果里面包含非法字符如冒号双引号等，那么就把他们消去，加到SB里面
            m.appendReplacement(sb, "");
            result = m.find();
        }
        m.appendTail(sb);
        String input2 = sb.toString();
        if (deletedIllegalChars) {
            System.out.println("输入的EMAIL地址里包含有冒号、逗号等非法字符，请修改");
            System.out.println("您现在的输入为: " + input);
            System.out.println("修改后合法的地址应类似: " + input2);
        }
    }
}

